﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;

namespace CFScript.Tasks
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class RegistryGet : Task
    {
        public RegistryGet()
        {
            this.name = "<registryGet>";
            this.parameterNames = new string[] { "key", "name", "varName" };
            this.parameterExpandable = new bool[] { true, true, true };
            this.parameterRequired = new bool[] { true, true, true };
            this.parameters = new Dictionary<string, Parameter>();

            Debug.Assert(this.parameterNames.Length == this.parameterRequired.Length &&
                         this.parameterRequired.Length == this.parameterRequired.Length);
        }

        public override InstructionResult Execute(CFScript context)
        {
            InstructionResult result = new InstructionResult();
            result.ExecutedInstruction = this;

            string key = this.parameters["key"].GetValue(context);
            string name = this.parameters["name"].GetValue(context);
            string varName = this.parameters["varName"].GetValue(context);

            try
            {
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(key);
                if (rk != null)
                {
                    try
                    {
                        varName = (string)rk.GetValue(name);

                        // TODO: CHIP: A esto le tendré que dar una vuelta porque seguramente habrá que montarlo de otra forma:
                        Variable v = Variable.VariableFactory(key + name, // Nombre de la variable --> Concateno key + name
                                                              varName,    // Valor de la clave del registro
                                                              "string");  // Tipo
                        
                        context.AddVariable(v);

                        result.Code = ResultCode.OK;
                    }
                    catch (Exception exc)
                    {
                        result.Code = ResultCode.ERROR;
                        result.Message = exc.Message;
                    }
                }
                else
                {
                    result.Code = ResultCode.ERROR;
                    switch (context.Options.VerboseLevel)
                    {
                        case VerboseMode.MINIMUM:
                        case VerboseMode.ALL:
                            result.Message = string.Format("Doesn't exist a registry value with this name: {0}", key);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERROR;
                result.Message = ex.Message;
            }

            result.BreakExecution = result.Code == ResultCode.ERROR && this.options.OnError == OnErrorAction.BREAK;

            return result;
        }
    }
}
